﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectTest.Models
{
    public class QLCauHoi
    {
        private DBTestDataContext db = new DBTestDataContext();

        //lay toan bo cau hoi
        public IEnumerable<CauHoi> GetAll()
        {
            return db.CauHois;
        }
        //them cau hoi theo
        public void Create(CauHoi qs)
        {
            try
            {
                db.CauHois.InsertOnSubmit(qs);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        //sau thong tin cau hoi
        public void Edit(CauHoi qs, int mach)
        {
            CauHoi ch = db.CauHois.SingleOrDefault(p => p.MaCH == mach);
            try
            {
                UpdateInfo(ch, qs);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void UpdateInfo(CauHoi ch, CauHoi qs)
        {
            ch.HinhAnh = qs.HinhAnh;
            ch.MaHP = ch.MaHP;
            ch.MoTa = ch.MoTa;
            ch.DapAnDung = ch.DapAnDung;
            ch.MaGV = ch.MaGV;
        }
        //xoa thong tin cau hoi theo ma
        public void Delete(int maCH)
        {
            CauHoi ch = db.CauHois.SingleOrDefault(p => p.MaCH == maCH);
            try
            {
                db.CauHois.DeleteOnSubmit(ch);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        //tim thong tin cau hoi theo ma

        
    }
}